﻿using Autofac;
using Newtonsoft.Json;
using Pay.Core;
using Pay.DTO;
using Pay.H5.Models;
using Pay.IService;
using System.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Pay.TO;

namespace Pay.H5.Controllers {
	[AppAuthorize("Member")]
	public class CheckOutController : GeneralController {
		private IOrderService _iOrderService;
		private IMerchantService _iMerchantService;
		private IRegionService _iRegionService;
		private IProductService _productService;
		private IGradeService _iGradeService;
		public CheckOutController(IOrderService iOrderService,IMerchantService iMerchantService,
		                          IRegionService iRegionService,IProductService iProductService,
		                          IGradeService iGradeService) {
			_iOrderService = iOrderService;
			_iMerchantService = iMerchantService;
			_iRegionService = iRegionService;
			_productService = iProductService;
			_iGradeService = iGradeService;
		}

		/// <summary>
		/// 【正常购买流程】确认订单
		/// </summary>
		/// <returns>The order.</returns>
		/// <param name="model">Model.</param>
		[AllowAnonymous]
		public ActionResult FirmOrder(string model) {
			var log = new LogHelper("info");
			bool is_seaover = false;
			var checkout_order = new CartModel();
			checkout_order.user_id = UserId;
			checkout_order.share_no = GetCookieValue("share_no");
			checkout_order.cache_no = GetCookieValue("cache_no");
			try {
				byte[] c = Convert.FromBase64String(model);
				model = System.Text.Encoding.Default.GetString(c);
				var listviewmodel = JsonConvert.DeserializeObject<List<ordermodel>>(model);
				checkout_order.ordermodel = listviewmodel;
			} catch (Exception ex) {
				log.Info(ex.Message);
				throw new ServerErrorException("请求数据报文错误");
			}

			var listorder = _iOrderService.CheckOut(checkout_order, out is_seaover);
			ViewData["url"] = site.img_url;
			ViewData["pay_no"] = (UserId > 0 ? "" : checkout_order.cache_no);
			ViewData["totalmoney"] = listorder.Sum(x => x.order_amount);//订单总金额=订单产品总金额+国内运费+国外运费+国外关税
			ViewData["tax"] = listorder.Sum(c => c.tax);//关税
			ViewData["freight"] = listorder.Sum(c => c.freight);//总运费
			ViewData["address"] = listorder.FirstOrDefault().shipping;//收货地址信息
			ViewData["list"] = checkout_order.ordermodel;//用户购买的产品
			ViewData["is_seaover"] = is_seaover;
			return View("~/Views/ShopCart/FirmOrder.cshtml");
		}

		/// <summary>
		/// 支付成功页面
		/// </summary>
		/// <returns></returns>
		[AllowAnonymous]
		public ActionResult PayFinish() {
			return View("~/Views/ShopCart/PayFinish.cshtml");
		}
		/// <summary>
		/// 订单支付
		/// </summary>
		/// <returns>The pay.</returns>
		/// <param name="pay_no">Pay no.</param>
		[AllowAnonymous]
		public async Task<JsonResult> Pay(string pay_no) {
			object object_id;
			var pay_model = new CartPayModel {
				pay_no = pay_no,
				user_id = UserId,
				merchant_no = RequestMethodh5.MerchantNO,
				method = RequestMethodh5.YZF,
				user_level_vip = RequestMethodh5.USER_LEVEL_VIP,
				user_level_top = RequestMethodh5.USER_LEVEL_TOP,
				notify = string.Format("{0}/pay/klt_success", RequestMethodh5.api_url),
				ip = Request.UserHostAddress,
				front_notify = "http://h5.epalpay.com/Checkout/PayFinish"
			};
			var model = _iOrderService.CheckOutPay(pay_model, out object_id);
			if (model.shipping == null) {
				api_result.result_msg = "请填写收货地址";
				api_result.result_code = 500;
				return Json(api_result, JsonRequestBehavior.AllowGet);
			}
			var result = await HChannelApi.Current.PostApi<mall_h5_result>(model);
			if (result == null) {
				api_result.result_msg = "网络异常";
				api_result.result_code = 500;
				return Json(api_result, JsonRequestBehavior.AllowGet);
			}

			if (result.result_code == 1000) {
				api_result.result_msg = result.code_url;
				api_result.result_code = 1000;
			} else {
				api_result.result_msg = result.result_msg;
				api_result.result_code = -1;
			}
			//_iOrderService.ClearOrderList(object_id);
			return Json(api_result, JsonRequestBehavior.AllowGet);
		}
	}
}
